package com.quanyan.operative.web;

import com.quanyan.api.APIResponse;
import com.quanyan.operative.model.req.ReqOpDynamicCommentItem;
import com.quanyan.operative.model.req.ReqOpDynamicItem;
import com.quanyan.operative.model.resp.*;
import com.quanyan.operative.service.OpActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import scala.Int;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by slong.guo on 2016/12/23.
 */
@RestController
@RequestMapping(value = "/api/operative/opact", method = RequestMethod.POST)
public class OpActivityController {

    private static Logger logger = LoggerFactory.getLogger(OpActivityController.class);

    @Autowired
    private OpActivityService opActivityService;

    @RequestMapping(value = "opActivities", method = RequestMethod.POST)
    public APIResponse<List<RespActivityItem>> getOpActivities(@RequestBody Map map, HttpServletRequest request){
        return opActivityService.getOpActivities();
    }

    @RequestMapping(value = "opActivityInfo", method = RequestMethod.POST)
    public APIResponse<RespActivityItem> getActivityInfo(@RequestBody Map map, HttpServletRequest request){
        if(!map.containsKey("activityID")){
            return APIResponse.returnFail("没有有效活动ID");
        }

        return opActivityService.getActivityInfo((int)map.get("activityID"));
    }


    @RequestMapping(value = "getRewardInfo", method = RequestMethod.POST)
    public APIResponse<RespRewardItem> getRewardInfo(@RequestBody Map map, HttpServletRequest request){
        if(!map.containsKey("rewardID")){
            return APIResponse.returnFail("参数错误");
        }

        return opActivityService.getRewardInfo((int)map.get("rewardID"));
    }

    @RequestMapping(value = "postDynamic", method = RequestMethod.POST)
    public APIResponse<RespPostDynamic> postDynamic(@RequestBody ReqOpDynamicItem reqOpDynamicItem, HttpServletRequest request){
        if(reqOpDynamicItem == null || !reqOpDynamicItem.isValid()){
            return APIResponse.returnFail("参数错误");
        }

        Integer uid = (Integer) request.getAttribute("uid");

        return opActivityService.postDynamic(uid == null ? 0 : uid, reqOpDynamicItem);

    }

    @RequestMapping(value = "deleteDynamic", method = RequestMethod.POST)
    public APIResponse deleteDynamic(@RequestBody Map map, HttpServletRequest request){
        if(!map.containsKey("activityID") || !map.containsKey("dynamicID")){
            return APIResponse.returnFail("参数错误");
        }
        Integer uid = (Integer) request.getAttribute("uid");
        return opActivityService.deleteDynamic(uid, (int)map.get("dynamicID"), (int)map.get("activityID"));
    }

    @RequestMapping(value = "getDynamicItem", method = RequestMethod.POST)
    public APIResponse<RespDynamicItem> getDynamicItem(@RequestBody Map map, HttpServletRequest request){

        Integer activityID = (Integer)map.get("activityID");
        Integer dynamicID = (Integer)map.get("dynamicID");
        if(activityID == null || dynamicID == null){
            return APIResponse.returnFail("参数错误");
        }

        Integer uid = (Integer) request.getAttribute("uid");
        return opActivityService.getDynamicItem(uid == null ? 0 : uid, activityID, dynamicID);
    }

    @RequestMapping(value = "getDynamicList", method = RequestMethod.POST)
    public APIResponse<RespDynamicList> getDynamicList(@RequestBody Map map, HttpServletRequest request){
        Integer activityID = (Integer)map.get("activityID");
        Integer type = (Integer)map.get("type");
        Integer pageNo = (Integer)map.get("pageNo");
        Integer perPage = (Integer)map.get("perPage");
        if(activityID == null || type == null || pageNo == null || perPage == null){
            return APIResponse.returnFail("参数错误");
        }

        Integer uid = (Integer) request.getAttribute("uid");
        return opActivityService.getDynamicList(uid == null ? 0 : uid, activityID, type, pageNo, perPage);
    }

    @RequestMapping(value = "getDynamicLikeList", method = RequestMethod.POST)
    public APIResponse<List<RespOpUserInfoItem>> getDynamicLikeList(@RequestBody Map map, HttpServletRequest request){
        Integer activityID = (Integer)map.get("activityID");
        Integer dynamicID = (Integer)map.get("dynamicID");
        Integer pageNo = (Integer)map.get("pageNo");
        Integer perPage = (Integer)map.get("perPage");
        if(activityID == null || dynamicID == null || pageNo == null || perPage == null){
            return APIResponse.returnFail("参数错误");
        }

        return opActivityService.getDynamicLikeList(dynamicID, activityID, pageNo, perPage);
    }


    @RequestMapping(value = "addDynamicReadAndLike", method = RequestMethod.POST)
    public APIResponse<RespDynamicReadAndLike> addDynamicReadAndLike(@RequestBody Map map, HttpServletRequest request){
        Integer activityID = (Integer)map.get("activityID");
        Integer dynamicID = (Integer)map.get("dynamicID");
        Integer type = (Integer)map.get("type");

        if(activityID == null || dynamicID == null || type == null){
            return APIResponse.returnFail("参数错误");
        }

        Integer uid = (Integer) request.getAttribute("uid");
        return opActivityService.addDynamicReadAndLike(uid == null ? 0 : uid, dynamicID, activityID, type);
    }

    @RequestMapping(value = "getCommentItem", method = RequestMethod.POST)
    public APIResponse<RespDynamicCommentItem> getCommentItem(@RequestBody Map map, HttpServletRequest request){
        Integer activityID = (Integer)map.get("activityID");
        Integer dynamicID = (Integer)map.get("dynamicID");
        Integer commentID = (Integer)map.get("commentID");

        if(activityID == null || dynamicID == null || commentID == null){
            return APIResponse.returnFail("参数错误");
        }

        return opActivityService.getCommentItem(commentID, dynamicID, activityID);
    }

    @RequestMapping(value = "getCommentList", method = RequestMethod.POST)
    public APIResponse<RespDynamicCommentList> getCommentList(@RequestBody Map map, HttpServletRequest request){
        Integer activityID = (Integer)map.get("activityID");
        Integer dynamicID = (Integer)map.get("dynamicID");
        Integer pageNo = (Integer)map.get("pageNo");
        Integer perPage = (Integer)map.get("perPage");

        if(activityID == null || dynamicID == null || pageNo == null || perPage == null){
            return APIResponse.returnFail("参数错误");
        }

        return opActivityService.getCommentList(dynamicID, activityID, pageNo, perPage);
    }

    @RequestMapping(value = "postComment", method = RequestMethod.POST)
    public APIResponse<RespPostComment> postComment(@RequestBody ReqOpDynamicCommentItem reqItem, HttpServletRequest request){
        if(reqItem == null || !reqItem.isValid()){
            return APIResponse.returnFail("参数错误");
        }

        Integer uid = (Integer) request.getAttribute("uid");

        return opActivityService.postComment(uid, reqItem);
    }

    @RequestMapping(value = "deleteComment", method = RequestMethod.POST)
    public APIResponse deleteComment(@RequestBody Map map, HttpServletRequest request){
        Integer activityID = (Integer)map.get("activityID");
        Integer dynamicID = (Integer)map.get("dynamicID");
        Integer commentID = (Integer)map.get("commentID");

        if(activityID == null || dynamicID == null || commentID == null){
            return APIResponse.returnFail("参数错误");
        }

        Integer uid = (Integer) request.getAttribute("uid");

        return opActivityService.deleteComment(uid, commentID, dynamicID, activityID);


    }
}
